<?php
//
//	file: includes/aucp/color_handler.php
//	begin: 30/04/2007
//	$Author: christopher $
//	$Revision: 29 $
//	$Date: 2008-07-19 15:59:35 +0000 (Sat, 19 Jul 2008) $
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	description: main color class

if (!defined('IN_PHPBB'))
{
	die("Hacking attempt");
}

// simplified color class for front-end use
class color_handler
{
	var $data;
	var $color_data;
	var $data_time;
	var $from_cache;

	function set_vars()
	{
		$this->group_ids = array(
			GROUP_REGISTERED => true,
			GROUP_ANONYMOUS => true,
			GROUP_SESSION => true,
		);
	}

	function generate_css()
	{
		global $template;

		$ar_color = array();

		foreach($this->color_data as $color)
		{
			$color_code = $color['color_code'];

			if ( !empty($color_code) )
			{
				$group_id = $color['group_id'];
				$hover_bold = $color['hover_bold'];
				$hover_italic = $color['hover_italic'];
				$hover_underline = $color['hover_underline'];
				$bold = $color['bold'];
				$italic = $color['italic'];
				$underline = $color['underline'];
				$label = 'username_color_' . $group_id;

				$temp = ".$label, a.$label, a.$label:link, a.$label:active, a.$label:visited {\n";
				$temp .= 'color : #' . $color_code . ";\n";
				$temp .= ($bold) ? "font-weight: bold;\n" : '';
				$temp .= ($italic) ? "font-style: italic;\n" : '';
				$temp .= ($underline) ? "text-decoration: underline;\n" : '';
				$temp .= "}\n";
				$ar_color[] = $temp;

				$temp = "a.$label:hover {\n";
				$temp .= 'color : #' . $color_code . ";\n";
				$temp .= ($hover_bold) ? "font-weight: bold;\n" : '';
				$temp .= ($hover_italic) ? "font-style: italic;\n" : '';
				$temp .= ($hover_underline) ? "text-decoration: underline;\n" : '';
				$temp .= "}\n";
				$ar_color[] = $temp;
			}
		}

		$color_css = '<style type="text/css">' . "\n";
		$color_css .= "<!--\n";
		$color_css .= implode("\n", $ar_color);
		$color_css .= "-->\n";
		$color_css .= "</style>\n";

		$template->assign_vars(array(
			'COLOR_CSS' => $color_css)
		);
	}

	function color_name($group, $session_time=0, $username)
	{
		return '<span class="' . $this->get_color($group, $session_time) . '">'. $username .'</span>';
	}

	function get_color($group, $session_time=0)
	{
		global $config;

		if ( !($session_time >= (time() -  $config->data['aucp_color_time'])) && $session_time)
		{
			$group = GROUP_SESSION;
		}
		return 'username_color_' . $group;
	}

	function read_style()
	{
		global $theme, $user;

		if (!$theme['themes_id'])
		{
			$user->set_style();
		}

		$this->color_data = $this->data[$theme['themes_id']];

	}

	function read($force=false)
	{
		global $config, $themes, $groups_list;

		if ( $force )
		{
			color_admin::update_group_weight();
			include_once($config->url('includes/class_forums'));

			$moderators = new moderators();
			$moderators->read(true);

			// make sure all themes are in the colors table
			$themes->read();
			foreach ($themes->data as $row)
			{
				color_admin::add(intval($row['themes_id']), 'theme');
			}

			// make sure all groups are in the colors table
			$groups = $group_list->list_groups();
			foreach($groups as $group_id)
			{
				color_admin::add($group_id);
			}

			// prune orphan groups
			$sql = 'SELECT group_id
				FROM ' . COLOR_TABLE . '
				ORDER BY group_id ASC';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__);

			while ( $row = $db->sql_fetchrow($result) )
			{
				if (!in_array($row['group_id'], $groups))
				{
					color_admin::remove($row['group_id']);
				}
			}
			$db->sql_freeresult($result);
		}

		$db_cached = new cache('dta_colors', $config->data['cache_path'], $config->data['cache_disabled_colors']);
		$sql = 'SELECT *
				FROM ' . COLOR_TABLE . '
				GROUP BY themes_id
				ORDER BY group_id ASC';
		$this->data = $db_cached->sql_query($sql, __LINE__, __FILE__, $force);
		$this->data_time = $db_cached->data_time;
		$this->from_cache = $db_cached->from_cache;
		unset($db_cached);

		return;
	}

	function display_legend()
	{
		global $template, $user, $config, $groups_list;

		$count = count($groups_list->data);
		foreach($groups_list as $group)
		{
			$count--;
			if($group['group_legend'])
			{
				$link_parm = array(POST_GROUPS_URL => $group['group_id']);

				$template->assign_block_vars('stats.legend', array(
					"U_GROUP" => $config->url('memberlist', $link_parm),
					"GROUP_DESCRIPTION" => $user->lang($group['group_description']),
					"GROUP_NAME" => $user->lang($group['group_name']),
					"GROUP_COLOR" =>  $this->get_color($group['group_id']))
				);

				if ( $count > 0 )
				{
					$template->assign_block_vars('stats.legend', array(
						"L_COMMA" => ',&nbsp;')
					);
				}
			}
		}
		return;
	}
}

// STATIC color class
class color_admin
{
	// adds group/theme $id to color table
	function add($id, $mode='group')
	{
		global $db, $colors;

		$id = intval($id);

		// get items that are already associated with this $mode
		if($mode == 'theme')
		{
			$sql = 'SELECT group_id as used_id
					FROM ' . COLOR_TABLE . '
					WHERE themes_id = ' . $id;
		}
		else
		{
			$sql = 'SELECT themes_id as used_id
					FROM ' . COLOR_TABLE . '
					WHERE group_id = ' . $id;
		}
		$result = $db->sql_query($sql, false, __LINE__, __FILE__, true);

		// populate that into an array
		$used_ids = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			$used_ids[ intval($row['used_id']) ] = true;
		}
		$db->sql_freeresult($result);

		// get all items available
		if($mode == 'theme')
		{
			$sql = 'SELECT group_id as use_id
					FROM ' . GROUPS_TABLE . '
					WHERE ( group_single_user <> ' . true . ' AND group_status < ' . GROUP_SPECIAL . ' )
					ORDER BY group_weight ASC';
		}
		else
		{
			$sql = 'SELECT themes_id as use_id
					FROM ' . THEMES_TABLE;
		}
		$result = $db->sql_query($sql, false, __LINE__, __FILE__, true);

		// include unassociated items
		$sql_insert = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			if ( !$used_ids[intval($row['use_id'])] )
			{
				$sql_insert[] = intval($row['use_id']);
			}
		}

		$db->sql_freeresult($result);

		// add orphaned items
		if($mode == 'theme')
		{
			$scheme = '(themes_id, group_id)';
		}
		else
		{
			$scheme = '(group_id, themes_id)';
		}
		$sql = 'INSERT INTO ' . COLOR_TABLE . "
			$scheme VALUES ($id," . implode("),\n($id,", $sql_insert) . ')';
		$db->sql_query($sql, false, __LINE__, __FILE__, true);

		return true;
	}

	// removes group/theme $id from color table
	function remove($id, $mode='group')
	{
		global $db, $colors;

		$type = ($mode =='theme') ? 'themes_id' : 'group_id';

		$sql = 'DELETE FROM ' . COLOR_TABLE . '
					WHERE ' . $type . ' = ' . intval($id);
		$db->sql_query($sql, false, __LINE__, __FILE__, true);

		return true;
	}

	function read_group_users($group_id, $new_group=false)
	{
		global $db;

		if ( $group_id == GROUP_REGISTERED )
		{
			return;
		}

		$users = array();

		$sql = 'SELECT ug.user_id
				FROM ' . USER_GROUP_TABLE . ' ug
					' . ( $new_group ? '' : 'LEFT JOIN ' . USERS_TABLE . ' u ON u.user_id = ug.user_id AND u.user_group_id = ' . intval($group_id) ). '
				WHERE ug.group_id = ' . intval($group_id) . '
					AND ug.user_pending <> 1
				GROUP BY ug.user_id';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__, true);

		while ( $row = $db->sql_fetchrow($result) )
		{
			$users[ intval($row['user_id']) ] = true;
		}
		$db->sql_freeresult($result);

		return $users;
	}

	function set_group_users($group_id, $users, $delete=false)
	{
		global $db, $config, $colors;

		$group_id = intval($group_id);

		if ( $group_id == GROUP_REGISTERED || empty($users) )
		{
			return;
		}

		// removes all $users from $group_id
		$sql = 'UPDATE ' . USERS_TABLE . '
				SET user_group_id = ' . GROUP_REGISTERED . '
				WHERE user_id IN (' . implode(', ' , $users) . ')
				AND user_group_id = ' . $group_id;
		$db->sql_query($sql, false, __LINE__, __FILE__, true);


		if ( in_array($config->data['stat_last_user'], $users) )
		{
			$config->set('stat_last_user_group_id', GROUP_REGISTERED);
		}
			$config->read(true);
			$colors->read(true);

		if ( !$delete )
		{
			return;
		}

		// sets all users found in group_id associated with GROUP_REGISTERED to group_id
		$users = array();
		$sql = 'SELECT user_id
				FROM ' . USER_GROUP_TABLE . '
				WHERE group_id = ' . $group_id . '
					AND user_pending <> 1
				GROUP BY user_id';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__, true);
		while ( $row = $db->sql_fetchrow($result) )
		{
			$users[] = intval($row['user_id']);
		}
		$db->sql_freeresult($result);

		$sql = 'UPDATE ' . USERS_TABLE . '
				SET user_group_id = ' . $group_id . '
				WHERE user_id IN (' . implode(', ' , $users)) . ')
				AND user_group_id = ' . GROUP_REGISTERED;
		$db->sql_query($sql, false, __LINE__, __FILE__, true);

		if ( in_array($config->data['stat_last_user'], $users) )
		{
			$config->set('stat_last_user_group_id', $group_id);
		}
		$config->read(true);
		$colors->read(true);
	}

	function set_colors($group_id = 0; $users = array())
	{
		global $db;

		$sql = 'SELECT group_id
				FROM ' . GROUPS_TABLE . '
				WHERE group_single_user <> ' . true . '
					AND group_color = 1
				ORDER BY group_id ASC';
		$group_result = $db->sql_query($sql, false, __LINE__, __FILE__, true);

		while ($grow = $db->sql_fetchrow($group_result))
		{
			$group_id = intval($grow['group_id']);

			$sql = 'SELECT user_id
					FROM ' . USER_GROUP_TABLE . '
					WHERE group_id = ' . $group_id . '
						AND user_pending <> 1
					GROUP BY user_id';
			$result = $db->sql_query($sql, false, __LINE__, __FILE__, true);

			$user_ids = array();
			while ($row = $db->sql_fetchrow($result))
			{
				if ( !in_array($row['user_id'], $user_ids) )
				{
					$user_ids[] = intval($row['user_id']);
				}
			}

			$db->sql_freeresult($result);

			if ( !empty($user_ids) )
			{
				$sql = 'UPDATE ' . USERS_TABLE . '
						SET user_group_id = ' . $group_id . '
						WHERE user_id IN (' . implode(', ' , array_keys($user_ids)) . ')
						AND user_group_id <> ' . GROUP_REGISTERED;
				$db->sql_query($sql, false, __LINE__, __FILE__, true);
			}
		}

		$db->sql_freeresult($group_result);
	}

	function update_weights()
	{
		global $db;

		$order = 0;

		$sql = 'SELECT group_id
				FROM ' . GROUPS_TABLE . '
				WHERE ( group_single_user <> ' . true . ' AND group_status < ' . GROUP_SPECIAL . ' )
				ORDER BY group_weight ASC';
		$result = $db->sql_query($sql, false, __LINE__, __FILE__, true);

		while ( $row = $db->sql_fetchrow($result) )
		{
			$order += 10;
			$sql = 'UPDATE ' . GROUPS_TABLE . '
					SET group_weight = ' . intval($order) . '
					WHERE group_id = ' . intval($row['group_id']);
			$db->sql_query($sql, false, __LINE__, __FILE__, true);
		}
		$db->sql_freeresult($result);

		return true;
	}

	function set_weight($group, $mode)
	{
		global $db;

		$value = (($mode == 'up') ? '+' : '-') . ' 15';

		$sql = 'UPDATE ' . GROUPS_TABLE . '
				SET group_weight = group_weight' . $value . '
				WHERE group_id = ' . $group;
		$db->sql_query($sql, false, __LINE__, __FILE__);

		return color_admin::update_weights();
	}

	function inactive_select()
	{
		global $config, $user;

		$timeout = $config->data['aucp_session_time'];

		$inactive = array(
			array('name' => $user->lang('AGCM_week'),
				'value' => 3600*24*7),
			array('name' => $user->lang('AGCM_day'),
				'value' => 3600*24),
			array('name' => $user->lang('AGCM_hour'),
				'value' => 3600),
			array('name' => $user->lang('AGCM_minute'),
				'value' => 60),
			array('name' => $user->lang('AGCM_second'),
				'value' =>1),
		);

		$inactive_select = '<select name="agcm_value">';
		$selected = false;

		foreach($inactive as $set)
		{
			if (($value_set == false) && ($timeout%$set['value'] = 0))
			{
				$selected = true;
			}
			$inactive_select .= '<option value="' . $set['value'] . '"' . (($selected) ?  ' selected="selected"' : '') . '>' . $set['name'] . '</option>';
		}

		$inactive_select .= '</select>';

		return $inactive_select;
	}

	function set_bots()
	{
		global $db;

		$bot_ids = array();
		$sql = 'SELECT bot_id
				FROM ' . BOTS_TABLE;
		$result = $db->sql_query($sql, false, __LINE__, __FILE__);

		while ( $row = $db->sql_fetchrow($result) )
		{
			$bot_ids[ intval($row['bot_id']) ] = true;
		}
		$db->sql_freeresult($result);

		$sql = 'UPDATE ' . USERS_TABLE . '
				SET user_group_id = ' . GROUP_BOTS . '
				WHERE user_id IN (' . implode(', ' , array_keys($bot_ids)) . ')';
		$db->sql_query($sql, false, __LINE__, __FILE__);
	}
}

?>